package com.eBook.controller;

import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eBook.entity.User;
import com.eBook.service.UserService;

@Controller
@RequestMapping(value = "/user")
public class UserController {
	@Autowired
	private UserService userService;
	/*
	 * 	用户登录，如果当前用户为第一次登录，将创建用户。
	 *	parameter account:用户帐号，前台参数名必须为account
	 *	return： 返回当前登录的用户
	 * */
	@RequestMapping(value = "/",method = RequestMethod.POST)
	@ResponseBody
	public User userLogin(String account,String name,String img,String vipDate){
		User user = new User(account,img,name,0,vipDate,0);
		return userService.userLogin(user);
	}
	/*
	 * 修改积分
	 * */
	@RequestMapping(value = "/bonus/{userId}/{bonus}",method = RequestMethod.POST)
	@ResponseBody
	public boolean updateBonus(@PathVariable("userId")int userId,
			@PathVariable("bonus")int bonus){
		Map<String, Integer> ub = new HashedMap();
		ub.put("userId", userId);
		ub.put("bonus", bonus);
		boolean flag = userService.updateBonus(ub);
		return flag;
	}
	/*
	 * 修改vip天数
	 * */
	@RequestMapping(value = "/vip/{userId}/{date}",method = RequestMethod.POST,
			produces = "application/json;charset=utf-8")
	@ResponseBody
	public boolean updateVip(@PathVariable("userId")int userId,
			@PathVariable("date")String date){
		Map<String, String> uv = new HashedMap();
		uv.put("userId", userId+"");
		uv.put("count", date);
		boolean flag = userService.updateVip(uv);
		return flag;
	}
	/*
	 * 修改签到天数
	 * */
	@RequestMapping(value = "/count/{userId}/{count}",method = RequestMethod.GET,
			produces = "application/json;charset=utf-8")
	@ResponseBody
	public boolean updateCount(@PathVariable("userId")int userId,
			@PathVariable("count")String count){
		Map<String, String> uv = new HashedMap();
		uv.put("userId", userId+"");
		uv.put("count", count);
		boolean flag = userService.updateCount(uv);
		return flag;
	}
}
